<?php

namespace Beerus\Modules\BrandActivity\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\model\brandActivity\BlindBoxLotteryConsignee;
use Biz\model\NewProduct\LogisticsLogModel;
use Biz\module\Packages\Logistics\TrackingMore;
use Biz\service\brand\BlindBoxActivity;
use Biz\service\brand\BlindBoxLottery;
use Biz\service\BrandActivity\BrandActivityService;
use Biz\service\findGoods\FineGoodsTestActivityJoinService;
use Biz\service\findGoods\FineGoodsTestActivityService;
use Biz\service\NewProduct\NewProductWikiListService;
use Biz\service\NewProduct\NewProductWinnerListService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class LogisticsController extends Controller
{
    public const  PAGE_SIZE = 20;

    /**
     * 获取列表
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function index(Request $request, Content $content): Content
    {
        $request_data = $request->all();
        $condition = self::getCondition($request_data);
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);
        $where = $condition;
        $where['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size,
        ];
        $list['list'] = BrandActivityService::logisticsIndex($where);

        //查询物流公司列表
        $courierList = NewProductWinnerListService::handleData(NewProductWinnerListService::getCourierList(),
            'trackingmore_code', 'courier_name');
        unset($where['LIMIT']);
        $list['total'] = LogisticsLogModel::count($where);
        $gridModel = new GridDataModel($list['list'], $list['total'], [
            'perPage' => $page_size,
        ]);
        $grid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $courierList) {
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function ($filter) use ($courierList) {
                    // 在这里添加字段过滤器
                    $filter->column(1 / 2, function ($filter) use ($courierList) {
                        $filter->equal('tracking_number', '物流单号')->placeholder('物流单号');
                        $filter->equal('carrier_code', '物流公司')->select($courierList);
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('logistics_channel', '活动类型')->select(TrackingMore::LOGISTICS_CHANNEL_NAME);
                        $filter->equal('logistics_status',
                            '物流状态')->select(LogisticsLogModel::STATUS_NAME_MAP);
                    });
                    $filter->expand();//默认展开
                });
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->column('id', '主键ID');
                $grid->column('tracking_number', '物流单号');
                $grid->column('carrier_code', '物流公司')->using($courierList);
                $grid->column('last_event', '物流最新进展')->modal('物流流转详情', function () {
                    $trackInfo = json_decode($this->track_info, true);
                    $data = [];
                    if ($trackInfo) {
                        foreach ($trackInfo as $item) {
                            $data[] = [
                                'StatusDescription' => $item['StatusDescription'],
                                'Date' => $item['Date'],
                                'Details' => $item['Details'],
                                'checkpoint_status' => LogisticsLogModel::STATUS_NAME_MAP[LogisticsLogModel::getStatus($item['checkpoint_status'])],
                            ];
                        }
                    }

                    return new Table(['状态描述', '日期', '地点详情', '物流状态'], $data);
                });
                $grid->column('lastest_checkpoint_time', '最新物流信息的更新时间');
                $grid->column('activity_name', '活动名称');
                $grid->column('logistics_channel', '活动类型')->using(TrackingMore::LOGISTICS_CHANNEL_NAME);
                $grid->column('logistics_status', '物流状态')->using(LogisticsLogModel::STATUS_NAME_MAP);
                $grid->column('addtime', '物流创建时间');
            }
        );
        $tab = new Tab();
        $tab->add('品牌活动-物流记录', $grid->render(), true);
        $content->row($tab);

        return $content
            ->header('品牌活动-物流记录')
            ->description(' ')
            ->breadcrumb(['text' => '品牌活动-物流记录', 'url' => '/brandActivity/logistics']);
    }

    /**
     * @param $request
     * @return array
     */
    private static function getCondition($request): array
    {
        $condition['ORDER'] = ['id' => 'DESC'];
        if (isset($request['tracking_number']) && !empty($request['tracking_number'])) {
            $condition['tracking_number'] = $request['tracking_number'];
        }
        if (isset($request['logistics_channel']) && !empty($request['logistics_channel'])) {
            $condition['logistics_channel'] = $request['logistics_channel'];
        }
        if (isset($request['carrier_code']) && !empty($request['carrier_code'])) {
            $condition['carrier_code'] = $request['carrier_code'];
        }
        if (isset($request['logistics_status']) && !empty($request['logistics_status'])) {
            $condition['logistics_status'] = $request['logistics_status'];
        }

        return $condition;

    }
}